package com.cskaoyan.controller;

import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.LoginUserVo;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.WxAuthRegBo;
import com.cskaoyan.bean.bo.WxAuthRegisterBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.UserInfoVo;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.service.WxAuthService;
import com.cskaoyan.utils.HttpUtil;
import com.cskaoyan.utils.CodeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author Fyy
 * @since 2023/01/01 18:29
 */

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {
    //验证码
    public static String code;
    @Autowired
    WxAuthService authService;


    /**
     * 小程序用户登录功能
     * @param map username,password
     * @return
     */
    @RequestMapping("login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");

        // 写业务
        Subject subject = SecurityUtils.getSubject();
        //执行登录认证
        MarketToken marketToken = new MarketToken(username,password,"wx");
        try {
            subject.login(marketToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        //修改最后登录时间
        marketUser.setLastLoginTime(new Date());
        //修改最后登录IP
        marketUser.setLastLoginIp(HttpUtil.getIp());
        authService.update(marketUser);
        //给返回值赋值
        LoginUserVo loginUserData = new LoginUserVo();
        UserInfoVo userInfo = new UserInfoVo();
        userInfo.setAvatarUrl(marketUser.getAvatar());
        userInfo.setNickName(marketUser.getUsername());
        loginUserData.setUserInfo(userInfo);
        loginUserData.setToken((String) subject.getSession().getId());
        return BaseRespVo.ok(loginUserData,"成功");
    }

    /**
     * 小程序登出功能
     * @return
     */
    @RequestMapping("logout")
    public BaseRespVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok("成功");
    }

    /**
     * 用户注册功能
     * @param registerBo
     * @return
     */
    @RequestMapping("register")
    public BaseRespVo register(@RequestBody WxAuthRegisterBo registerBo){
        //获取前端输入的验证码
        String code = registerBo.getCode();
        //判断手机号是否已注册
        int num = authService.select(registerBo);
        if (num == 1){
            return BaseRespVo.errMsgAndNo(705,"手机号已注册");
        }
        //注册的默认头像
        registerBo.setAvatar("http://localhost:8083/pic/4292a576-a87b-4f79-b22e-04371ec4754e.jpg");
        //更新添加时间
        registerBo.setAddTime(new Date());
        //更新修改时间
        registerBo.setUpdateTime(new Date());
        //更新最后登录ip
        registerBo.setLastLoginIp(HttpUtil.getIp());
        //设置Nickname
        registerBo.setNickname(registerBo.getUsername());
        //更新最后登录时间
        registerBo.setLastLoginTime(new Date());
        if (code.equals(this.code)){
            authService.register(registerBo);
        }else {
            return BaseRespVo.errMsgAndNo(703,"验证码错误");
        }
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装返回值
        LoginUserVo loginUserData = new LoginUserVo();
        UserInfoVo userInfo = new UserInfoVo();
        userInfo.setAvatarUrl(registerBo.getAvatar());
        userInfo.setNickName(registerBo.getUsername());
        loginUserData.setUserInfo(userInfo);
        loginUserData.setToken((String) subject.getSession().getId());
        return BaseRespVo.ok(loginUserData,"成功");
    }

    /**
     * 发送验证码功能
     * @param wxAuthRegBo
     * @return
     */
    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody WxAuthRegBo wxAuthRegBo){
        //获取传入的手机号
        String mobile = wxAuthRegBo.getMobile();
        //获取需要发送的验证码
        code = CodeUtil.getVerificationCode();
        //发送验证码
        authService.regCaptcha(mobile, code);
        return BaseRespVo.ok("成功");
    }

    /**
     * 忘记密码修改密码功能
     * @param registerBo
     * @return
     */
    @RequestMapping("reset")
    public BaseRespVo reset(@RequestBody WxAuthRegisterBo registerBo){
        //查询数据库内是否有相同信息的用户个数
        int num = authService.select(registerBo);
        //如果用户个数为0
        if (num == 0){
            return BaseRespVo.errMsgAndNo(706,"手机号未注册");
        }
        //获取客户端传来的验证码
        String code = registerBo.getCode();
        //更新最后登录时间
        registerBo.setLastLoginTime(new Date());
        //更新最后登录ip
        registerBo.setLastLoginIp(HttpUtil.getIp());
        //更新最后修改时间
        registerBo.setUpdateTime(new Date());
        //判断发送出去的验证码和客户端传入的验证码是否相同
        if (code.equals(this.code)){
            //相同就修改密码
            authService.updateByMobile(registerBo);
        }else {
            //不同返回验证码错误
            return BaseRespVo.errMsgAndNo(703,"验证码错误");
        }
        return BaseRespVo.ok("成功");
    }
}
